package com.ruipengkj.view.memberView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TCabinet;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.MemberCabinetModel;
import com.ruipengkj.service.TCabinetService;
import com.ruipengkj.view.base.BasePagePanel;
//集成分页组件
public class CabinetPanel extends BasePagePanel<TCabinet> implements Runnable{
		private static final long serialVersionUID = 1L;
		private JLabelJTextFieldComponent name = null; //商柜名称
		
		private JButton queryButton = null;//查询按钮
		private JButton cleateButton = null;//清空按钮
		private JButton addButton = null;//添加按钮
		private JButton deleteButton = null;//删除按钮
		
		
		private JTable table;
		private MemberCabinetModel memberCabinetModel;
		private PagingComponent pagingComponent;//翻页组件
		private PageView<TCabinet> pageView;//翻页主键
		private JScrollPane tableJsp;
		private TCabinetService tCabinetService;
		private MemberCabinetAdd memberCabinetAdd;//会员详细窗体
		private MemberCabinetUpdate memberCabinetUpdate;//修改新闻
		public CabinetPanel() {
			tCabinetService=BuildSpringApplication.getInstance().getTCabinetServiceImpl();
			this.setLayout(new BorderLayout());
			this.init();
			this.initListener();
		}
		private void initListener() {
			// TODO Auto-generated method stub
			queryButton.addActionListener(this);
			cleateButton.addActionListener(this);
			deleteButton.addActionListener(this);
			addButton.addActionListener(this);
		}
		private void init() {
			// TODO Auto-generated method stub
			pageView=new PageView<TCabinet>();
			memberCabinetModel=new MemberCabinetModel();
			table=uiFactory.createJTable(memberCabinetModel, null, this, 30, JLabel.CENTER);
			//table设置列宽度 AUTO_RESIZE_SUBSEQUENT_COLUMNS 填满父容器  JTable.AUTO_RESIZE_OFF 自适应
			table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			tableJsp=new JScrollPane(table);
			pagingComponent=new PagingComponent();
			pagingComponent.setPageDelegale(this);
			
			this.add(this.topPanel(), BorderLayout.NORTH);
			this.add(tableJsp, BorderLayout.CENTER);
			this.add(pagingComponent, BorderLayout.SOUTH);
			this.termQuery(1);
			//排序
			TableRowSorter<MemberCabinetModel> tableRowSorter = new TableRowSorter<MemberCabinetModel>(memberCabinetModel);  
			table.setRowSorter(tableRowSorter);
		}
		private JPanel topPanel() {
			JPanel jp=new JPanel(new FlowLayout(FlowLayout.LEFT));
			name = new JLabelJTextFieldComponent("商柜名称:",16);
			queryButton = new JButton("查询");
			cleateButton= new JButton("清空");
			deleteButton = new JButton("删除");
			addButton = new JButton("添加");
			jp.add(name);
			jp.add(queryButton);
			jp.add(cleateButton);
			jp.add(deleteButton);
			jp.add(addButton);
			return jp;
		}
		@Override
		public void run() {
			this.termQuery(1);
		}
		//条件查询
		protected void termQuery(final int currentPage) {
			final TCabinet tCabinet=new TCabinet();
			tCabinet.setName(name.getText().getText().trim());
			new Thread(){
				public void run(){
					tableJsp.getViewport().remove(table);
					tableJsp.getViewport().add(waitComponent);
					try {
						pageView.setCurrentPage(currentPage);
						PageModel<TCabinet> pageModel=tCabinetService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tCabinet);
						pageView.setPageModel(pageModel);
						pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
						if(pageModel!=null){
							Util.clearTableRowSorter(table);
							memberCabinetModel.setRowData(pageModel.getResultList());
							SwingUtilities.invokeLater(new Runnable() {
								@Override
								public void run() {
									tableJsp.getViewport().remove(waitComponent);
									tableJsp.getViewport().add(table);
									Util.setJTabelAutoClumnWidth(table);
									pagingComponent.show();
								}
							});
						}else{
							//没有优惠卷信息
							waitComponent.setText("不存在数据");
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.start();
		}
		@Override
		protected PageView<TCabinet> getPageView() {
			return pageView;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==queryButton){
				
				//查询
				this.termQuery(1);
				
			}else if(e.getSource()==addButton){
				//添加
				memberCabinetAdd=new MemberCabinetAdd(null, "添加商柜", true);
				memberCabinetAdd.showDialog();
				if(memberCabinetAdd.isSuccess==true){
					this.termQuery(1);
				}
			}else if(e.getSource()==cleateButton){
				//清除
				name.getText().setText("");
			}else if(e.getSource()==deleteButton){
				int row = table.getSelectedRow();
				if (row != -1) {
				String id=table.getValueAt(table.getSelectedRow(), 0).toString();
				try {
					tCabinetService.deleteService(id);
					JOptionPane.showMessageDialog(null, "删除成功");
					this.termQuery(1);
					table.updateUI();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "删除失败（请确定商品未被其他数据库表使用）");
				}
				}else{
					JOptionPane.showMessageDialog(null, "请选择行在进行删除");
					return;
				}
			}
		}
		@Override
		public void keyReleased(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				/**
				if(e.getSource()==startTimeTF.getDate()||e.getSource() == timeTF.getDate()){
					//查询会员
					queryButton.doClick();
				}*/
				if(e.getSource()==name.getText()){
					//查询会员
					queryButton.doClick();
				}
			}
		}
		// 鼠标双击 显示详细
		 
		@Override
		public void mouseClicked(MouseEvent e) {
	    	if(e.getSource()==table&&e.getClickCount() == 2){
	    		//表格
	    		String id=table.getValueAt(table.getSelectedRow(), 0).toString();
	    		try {
	    			TCabinet tCabinet=tCabinetService.getByIdService(id);
					if(tCabinet==null){
						JOptionPane.showMessageDialog(null, "商柜不存在");
						return;
					}
					memberCabinetUpdate=new MemberCabinetUpdate(null, "修改页面", true);
					memberCabinetUpdate.showDialog(tCabinet);
					if(memberCabinetUpdate.isSuccess==true){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
	    	}
	    }
		@Override
		public void clear() {
			// TODO Auto-generated method stub
			
		}
}
